

#include "cv.h"
#include "VideoFrameObservable.h"

#include "highgui.h"
#include <stdio.h>
#include "MouseObserver.h"

// A Simple Camera Capture Framework
int main()
{
	CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);

	if (!capture)
	{
		fprintf(stderr, "ERROR: capture is NULL \n");
		return -1;
	}

	IplImage* frame = cvQueryFrame(capture);
	if (!frame)
	{
		fprintf(stderr, "ERROR: frame is null...\n");
		getchar();
		return -1;
	}

	VideoFrameObservable video(frame);
	IObserver *mouse = new MouseObserver();
	video.add(*mouse);
	video.reduce();


	// Create a window in which the captured images will be presented
	cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE);
	// Show the image captured from the camera in the window and repeat
	while (1)
	{
		// Get one frame
		IplImage* frame = cvQueryFrame(capture);
		if (!frame)
		{
			fprintf(stderr, "ERROR: frame is null...\n");
			getchar();
			break;
		}
		video.setFrame(frame);
		video.reduce();
		//mouse->handleEvent(0,10,10);
		// Do not release the frame!
		//If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
		//remove higher bits using AND operator
		if ((cvWaitKey(10) & 255) == 27)
		{
			if (video.isCalibrated())
			{
				break;
			}
			else
			{
				video.calibrate();
			}
		}
		else
		{
			video.DrawMarkers();
		}
		cvShowImage("mywindow", frame);
	}
	// Release the capture device housekeeping*/
	cvReleaseCapture(&capture);
	cvDestroyWindow("mywindow");

	delete mouse;
	return 0;
}
